package com.platform.web.controller.data;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.BaseLocation;
import com.platform.base.domain.UnitValveInfo;
import com.platform.base.domain.ValveInfo;
import com.platform.base.service.IBaseLocationService;
import com.platform.base.service.IUnitValveInfoService;
import com.platform.base.service.IValveInfoService;
import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.data.domain.UnitValveData;
import com.platform.data.service.IUnitValveDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 阀门采集数据Controller
 * 
 * @author wangqi
 * @date 2020-09-27
 */
@Controller
@RequestMapping("/data/unitValve")
public class UnitValveDataController extends BaseController
{
    private String prefix = "data/unitValve";

    @Autowired
    private IUnitValveDataService unitValveDataService;

    @Autowired
    private IUnitValveInfoService unitValveInfoService;

    @Autowired
    private IBaseLocationService baseLocationService;

    @RequiresPermissions("data:unitValve:view")
    @GetMapping(value = { "", "/{valveId}" })
    public String data(@PathVariable(value = "valveId", required = false) String valveId, ModelMap mmap)
    {
        if(valveId==null){
            valveId = "";
        }
        mmap.put("valveId", valveId);
        mmap.put("location", new BaseLocation());
        return prefix + "/data";
    }

    /**
     * 根据地址查询阀门信息
     * @param locationId
     * @param mmap
     * @return
     */
    @RequiresPermissions("data:unitValve:view")
    @GetMapping("/location/{locationId}")
    public String location(@PathVariable(value = "locationId") String locationId, ModelMap mmap)
    {
        String valveIds = "";
        List<UnitValveInfo> list = unitValveInfoService.list(new QueryWrapper<UnitValveInfo>().eq("location_id", locationId));
        for(UnitValveInfo unitValveInfo : list){
            if(valveIds.equals("")){
                valveIds = unitValveInfo.getValveId();
            }else{
                valveIds = valveIds + "," + unitValveInfo.getValveId();
            }
        }
        mmap.put("valveId", valveIds);
        mmap.put("location", baseLocationService.getById(locationId));
        return prefix + "/data";
    }

    /**
     * 查询阀门采集数据列表
     */
    @RequiresPermissions("data:unitValve:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UnitValveData unitValveData)
    {
        startPage();
        if(unitValveData.getValveId() !=null && !unitValveData.getValveId().equals("")){
            unitValveData.setLocationId(null);
        }
        List<UnitValveData> list = unitValveDataService.selectUnitValveDataList(unitValveData);
        return getDataTable(list);
    }

    /**
     * 导出阀门采集数据列表
     */
    @RequiresPermissions("data:unitValve:export")
    @Log(title = "阀门采集数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UnitValveData unitValveData)
    {
        if(unitValveData.getValveId() !=null && !unitValveData.getValveId().equals("")){
            unitValveData.setLocationId(null);
        }
        List<UnitValveData> list = unitValveDataService.selectUnitValveDataList(unitValveData);
        ExcelUtil<UnitValveData> util = new ExcelUtil<UnitValveData>(UnitValveData.class);
        return util.exportExcel(list, "阀门采集数据");
    }

    /**
     * 读取阀门数据
     */
    @Log(title = "读取阀门数据", businessType = BusinessType.OTHER)
    @RequiresPermissions("data:unitValve:check")
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult check(String valveId) throws InterruptedException {

        String message = unitValveDataService.checkData(valveId);
        return AjaxResult.success(message);
    }

    /**
     * 设置阀门开度
     */
    @Log(title = "设置阀门开度", businessType = BusinessType.OTHER)
    @RequiresPermissions("data:unitValve:aperture")
    @PostMapping("/aperture")
    @ResponseBody
    public AjaxResult aperture(UnitValveInfo unitValveInfo,String valveId) throws InterruptedException {
        String hex = NettyUtil.get16HexNum(unitValveInfo.getAperture());
        if(hex.length()<2){
            hex = "0" + hex;
        }
        String message = unitValveDataService.apertureData(valveId, hex);
        return AjaxResult.success(message);
    }
}
